/**
 * Created by Administrator on 2017/8/29.
 */
var pkg = require('./package.json')
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var OpenBrowserPlugin = require('open-browser-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var loaders = [
    {
        loader: "css-loader",
        options: {
            modules: true
        }
    },{
        loader: "postcss-loader"
    }
];

module.exports = {
    devtool:'source-map',
    entry:{
      app:path.resolve(__dirname,'app/index.jsx'),
      vendor:Object.keys(pkg.dependencies),
    },
    output: {
        path: __dirname + "/build",
        filename: "js/[name].[chunkhash:8].js"
    },
    resolve:{
        extensions:['.js','.jsx']
    },
    module:{
        rules: [
            {
                test: /\.(js|jsx)$/,
                use: {
                    loader: "babel-loader",
                },
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: loaders,
                }),
                exclude: /node_modules/
            },
            {
                test: /\.less$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: loaders.concat({loader: "less-loader"}),
                }),
                exclude: /node_modules/
            },
            {
                test: /\.(png|gif|jpg|jpeg|bmp)$/,
                use: [
                    {
                        loader: "url-loader?limit=5000"
                    }
                ],
                exclude: /node_modules/
            },
            {
                test: /\.(png|woff|woff2|svg|ttf|eot)($|\?)/i,
                use: [
                    {
                        loader: "url-loader?limit=5000"
                    }
                ],
                exclude: /node_modules/
            }// 限制大小小于5k
        ]
    },
    plugins: [
        // html 模板插件
        new HtmlWebpackPlugin({
            template: __dirname+'/app/index.html'
        }),
        // 热加载插件
        new webpack.HotModuleReplacementPlugin(),
        // 打开浏览器
        new OpenBrowserPlugin({
            url: 'http://localhost:8080'
        }),
        new webpack.optimize.OccurrenceOrderPlugin(),
        //压缩代码，warnings不打印
        new webpack.optimize.UglifyJsPlugin({
            compress:{
                warnings:false
            }
        }),
        //分离css和js文件
        new ExtractTextPlugin('/css/[name].[chunkhash:8].css'),
        //提供公共代码
        new webpack.optimize.CommonsChunkPlugin({
            name:'vendor',
            filename:'/js/[name].[hash:8].js'
        }),
        //定义环境变量使react压缩到最小
        new webpack.DefinePlugin({
            'process.env':{
                NODE_ENV: JSON.stringify(JSON.stringify(process.env.NODE_ENV))
            }
        }),
        // 可在业务 js 代码中使用 __DEV__ 判断是否是dev模式（dev模式下可以提示错误、测试报告等, production模式不提示）
        new webpack.DefinePlugin({
            __DEV__: JSON.stringify(JSON.parse((process.env.NODE_ENV == 'dev') || 'false'))
        })

    ],
    devServer: {
        contentBase: "./build", //本地服务器所加载的页面所在的目录
        // colors: true, //终端中输出结果为彩色
        historyApiFallback: true, //不跳转
        inline: true, //实时刷新
        hot: true  // 使用热加载插件 HotModuleReplacementPlugin
    }
}